﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing.ParamForm
{
	public partial class LocalResistanceForm : System.Windows.Forms.Form
	{
		public LocalResistanceForm(HYPipeData hyPipeData)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.pipeData = hyPipeData;
			this.newPipeData = new HYPipeData(hyPipeData);
		}

		private void LocalResistanceForm_Load(object sender, EventArgs e)
		{
			this.LoadData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.numGateValve.Text == "" || this.numGlobeValve.Text == "" || this.numXieGan.Text == "" || this.numElbow.Text == "" || this.numYiZiWan.Text == "" || this.numKuoWan.Text == "" || this.numSquare.Text == "" || this.numCasing.Text == "" || this.hyTxtHeatingLR.Text == "" || this.hyTxtOther.Text == "")
			{
				YJKMessageBox.Information("参数不能为空。");
				return;
			}
			this.SaveData();
			base.DialogResult = DialogResult.OK;
		}

		private void LoadData()
		{
			int num = 0;
			int num2 = 0;
			foreach (ValveAndFitting valveAndFitting in this.newPipeData.ValvesList)
			{
				ValveString valveStringByName = GHCalcMethods.GetValveStringByName(valveAndFitting.VFName);
				switch (valveStringByName)
				{
				case ValveString.闸阀:
					this.numGateValve.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.截止阀:
					this.numGlobeValve.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.斜杆截止阀:
					this.numXieGan.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.弯头:
					this.numElbow.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.乙字弯:
					this.numYiZiWan.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.扩弯:
					this.numKuoWan.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.方形补偿器:
					this.numSquare.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.套管补偿器:
					this.numCasing.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.散热器:
					this.hyTxtHeatingLR.Text = valveAndFitting.Value.ToString();
					break;
				case ValveString.其他:
					this.hyTxtOther.Text = valveAndFitting.Value.ToString();
					break;
				}
				if (valveStringByName.ToString().Contains("三通"))
				{
					num++;
				}
				if (valveStringByName.ToString().Contains("四通"))
				{
					num2++;
				}
			}
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
			this.lbNotes.Text = string.Concat(new object[]
			{
				"其中 三通：",
				num,
				"个  四通：",
				num2,
				"个"
			});
			this.lbNotes.ForeColor = Color.Red;
			this.ValueChange();
		}

		private void SaveData()
		{
			this.AddList(this.numGateValve.Text, ValveString.闸阀.ToString(), this.pipeData);
			this.AddList(this.numGlobeValve.Text, ValveString.截止阀.ToString(), this.pipeData);
			this.AddList(this.numXieGan.Text, ValveString.斜杆截止阀.ToString(), this.pipeData);
			this.AddList(this.numElbow.Text, ValveString.弯头.ToString(), this.pipeData);
			this.AddList(this.numYiZiWan.Text, ValveString.乙字弯.ToString(), this.pipeData);
			this.AddList(this.numKuoWan.Text, ValveString.扩弯.ToString(), this.pipeData);
			this.AddList(this.numSquare.Text, ValveString.方形补偿器.ToString(), this.pipeData);
			this.AddList(this.numCasing.Text, ValveString.套管补偿器.ToString(), this.pipeData);
			this.AddList(this.hyTxtHeatingLR.Text, ValveString.散热器.ToString(), this.pipeData);
			this.AddList(this.hyTxtOther.Text, ValveString.其他.ToString(), this.pipeData);
		}

		private void AddList(string strText, string strValve, HYPipeData hyPipeData)
		{
			for (int i = 0; i < hyPipeData.ValvesList.Count; i++)
			{
				if (hyPipeData.ValvesList[i].VFName == strValve)
				{
					hyPipeData.ValvesList.RemoveAt(i);
				}
			}
			if (double.Parse(strText) != 0.0)
			{
				hyPipeData.ValvesList.Add(new ValveAndFitting(strValve, double.Parse(strText)));
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void ValueChange()
		{
			this.numGateValve.ValueChanged += this.numGateValve_ValueChanged;
			this.numGlobeValve.ValueChanged += this.numGlobeValve_ValueChanged;
			this.numXieGan.ValueChanged += this.numXieGan_ValueChanged;
			this.numElbow.ValueChanged += this.numElbow_ValueChanged;
			this.numYiZiWan.ValueChanged += this.numYiZiWan_ValueChanged;
			this.numKuoWan.ValueChanged += this.numKuoWan_ValueChanged;
			this.numSquare.ValueChanged += this.numSquare_ValueChanged;
			this.numCasing.ValueChanged += this.numCasing_ValueChanged;
			this.hyTxtHeatingLR.TextChanged += this.hyTxtHeatingLR_TextChanged;
			this.hyTxtOther.TextChanged += this.hyTxtOther_TextChanged;
		}

		private void hyTxtOther_TextChanged(object sender, EventArgs e)
		{
			this.AddList(this.hyTxtOther.Text, ValveString.其他.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void hyTxtHeatingLR_TextChanged(object sender, EventArgs e)
		{
			this.AddList(this.hyTxtHeatingLR.Text, ValveString.散热器.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numCasing_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numCasing.Value.ToString(), ValveString.套管补偿器.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numSquare_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numSquare.Value.ToString(), ValveString.方形补偿器.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numKuoWan_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numKuoWan.Value.ToString(), ValveString.扩弯.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numYiZiWan_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numYiZiWan.Value.ToString(), ValveString.乙字弯.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numElbow_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numElbow.Value.ToString(), ValveString.弯头.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numXieGan_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numXieGan.Value.ToString(), ValveString.斜杆截止阀.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numGlobeValve_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numGlobeValve.Value.ToString(), ValveString.截止阀.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private void numGateValve_ValueChanged(object sender, EventArgs e)
		{
			this.AddList(this.numGateValve.Value.ToString(), ValveString.闸阀.ToString(), this.newPipeData);
			this.hyTxtLocalResistance.Text = this.newPipeData.ResCoefLocal.ToString();
		}

		private HYPipeData newPipeData;

		private HYPipeData pipeData;
	}
}
